package com.hxzy.service.impl;

import com.hxzy.entity.PmsBrand;
import com.hxzy.mapper.PmsBrandMapper;
import com.hxzy.service.PmsBrandService;
import com.hxzy.util.MybatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class PmsBrandServiceImpl implements PmsBrandService {
    @Override
    public PmsBrand selectByPrimaryKey(Long id) {
        PmsBrand pmsBrand=null;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            PmsBrandMapper mapper = session.getMapper(PmsBrandMapper.class);
            pmsBrand=mapper.selectByPrimaryKey(id);
        }
        return pmsBrand;
    }

    @Override
    public boolean insert(PmsBrand record) {
        int i = 0;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            PmsBrandMapper mapper = session.getMapper(PmsBrandMapper.class);
            i=mapper.insert(record);
            session.commit();//增删改都要提交事务
        }
        return i>0;
    }

    @Override
    public boolean updateByPrimaryKey(PmsBrand record) {
        int i = 0;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            PmsBrandMapper mapper = session.getMapper(PmsBrandMapper.class);
            i=mapper.updateByPrimaryKey(record);
            session.commit();//增删改都要提交事务
        }
        return i>0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(PmsBrand record) {
        int i = 0;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            PmsBrandMapper mapper = session.getMapper(PmsBrandMapper.class);
            i=mapper.updateByPrimaryKey(record);
            session.commit();//增删改都要提交事务
        }
        return i>0;
    }

    @Override
    public List<PmsBrand> search() {
        List<PmsBrand> search=null;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            PmsBrandMapper mapper = session.getMapper(PmsBrandMapper.class);
            search = mapper.search();
            session.commit();
        }
        return search;
    }

    @Override
    public List<PmsBrand> searchByCondition(PmsBrand pmsBrand) {
        List<PmsBrand> list;
        try (SqlSession session= MybatisUtil.getSqlSessionFactory().openSession()){
            PmsBrandMapper mapper = session.getMapper(PmsBrandMapper.class);
            list = mapper.searchByCondition(pmsBrand);
        }
        return list;
    }
}
